# 26. 剩余银饰的重量

26

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});
let lines = [];

rl.on('line', function(line) {
    lines.push(line);
    if (lines.length === 2) {
        let silverPieces = lines[1].split(' ').map(Number);
        silverPieces.sort((a, b) => b-a);
        while(silverPieces.length >= 3) {
            let z = silverPieces.shift();
            let y = silverPieces.shift();
            let x = silverPieces.shift();
            if (x === y && y === z) {
                continue;
            } else {
                let remaining;
                if (x === y && y<z) {
                    remaining = z - y;
                } else if (x < y && y === z) {
                    remaining = y - x;
                } else {
                    remaining = Math.abs((z - y) - (y - x));
                }
                if (remaining !== 0) {
                    silverPieces.push(remaining);
                }
                silverPieces.sort((a, b) => b - a);
            }
        }
        if (silverPieces.length === 0) {
            console.log(0);
        } else {
            console.log(silverPieces[0]);
        }
    }
})

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41